import Vue from 'vue'
import Vuex from 'vuex'
import {
	toLogin,
	toLogout,
} from "@/api/login.js"

Vue.use(Vuex)

const store = new Vuex.Store({
	// 管理用户数据
	state: {
		// 登录状态
		hasLogin: false,
		// 是否是管理员
		is_manger: 0,
		// 用户token
		token: null,
		// 用户信息
		userInfo: {},
		// 当前徽章信息
		badgeName: null,
	},
	// 用于计算属性
	getters: {

	},
	mutations: {
		updateState(state, data) {
			// {
			//     "code": 0,
			//     "msg": "success",
			//     "data": {
			//         "is_manger": 1,
			//         "user": {
			//             "nickname": "测试001",
			//             "phone": ""
			//         },
			//         "token": "viH4Tcbjhaso7mKSsAfeAjU5wy14rqny"
			//     }
			// }

			localStorage.setItem('USER_INFO', JSON.stringify(data.user))

			localStorage.setItem('TOKEN', data.token)
			localStorage.setItem('IS_MANGER', data.is_manger)
			localStorage.setItem('HASLOGIN', "1")

			state.userInfo = formatData(data.user)
			state.token = data.token
			state.is_manger = data.is_manger
			state.hasLogin = true
			//持久化存储
			uni.setStorageSync("userInfo", JSON.stringify(state.userInfo))
			console.log("-=-=-=..////");
			console.log(state);
		},
		updateUserInfo(state, data) {
			console.log("---------------", data);
			state.userInfo = formatData(data)
			console.log("---================---", state);
			//持久化存储
			uni.setStorageSync("userInfo", JSON.stringify(state.userInfo))
		},
		// 更新当前徽章信息
		updateBadge(state, data) {
			state.badgeName = data
		},
		login(state, openid, iv, encryptedData, inviter) {
			console.log("---///--", state, openid, iv, encryptedData, inviter);


			toLogin(openid, iv, encryptedData, inviter).then(res => {
				// 返回token
				// uni.setStorageSync("token", res)
				state.hasLogin = true;
				state.userInfo = res.userInfo
				console.log("--------77--------");
				console.log(res);
				console.log(state);
				console.log(provider);
				console.log("--------77--------");
			})


			// state.userInfo = provider;
			// uni.setStorage({ //缓存用户登陆状态
			// 	key: 'userInfo',
			// 	data: provider
			// })
			console.log("----------------");
			console.log(state.userInfo);
		},
		//退出登录
		logout(state) {

			state.hasLogin = false
			state.token = null
			state.userInfo = {}
			state.badgeName = null;
			//清除存储
			uni.removeStorageSync("userInfo")
		}
	},
	actions: { //可同步，可异步
		add(context, s) {
			context.commit('up', s)
			console.log("add")
		},
		reduce(context) {
			var s = 5
			context.commit('down', s)
			// setTimeout(()=>{  //5秒钟后跳转
			//     router.push({path:"/E"})
			// },5000)
		}

	},
})

function formatData(data) {
	// 格式化用户信息
	const date = new Date(data.memberExpirationDate);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();

	return {
		id: data.id,
		openid: data.openid,
		avatar: data.avatar ? data.avatar : '',
		name: data.name ? data.name : "昵称",
		age: data.age,
		sex: data.sex,
		sexStr: data.sexStr,
		education: data.education,
		educationStr: data.educationStr,
		phoneNumber: data.phoneNumber,
		kindergartenName: data.kindergartenName,
		title: data.title,
		hobby: data.hobby,
		position: data.position,
		growthValue: data.growthValue,
		memberId: data.memberId,
		memberType: data.memberType,
		memberTypeStr: data.memberTypeStr,
		memberExpirationDate: `${year}-${month}-${day}`,
		aiUseCount: data.aiUseCount,
	};
}
export default store